<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle异常类文件
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id$
 * @package exception
 */
/**
 * 异常处理类
 *
 * @package	exception
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
class TException extends Exception
{
	private $_errorCode 	= '';
	private $lang			= 'en';
	private $_messagePath	= '';
	/**
	 * 构造函数.
	 * @param string error message. This can be a string that is listed
	 */
	public function __construct($errorMessage)
	{
		$this->lang 		= (Configure::item('language')!=false)?Configure::item('language'):$this->lang;
		$this->_messagePath = BASEPATH."/languages/{$this->lang}";
		$this->_errorCode 	= $errorMessage;
		$errorMessage		= $this->translateErrorMessage($errorMessage);
		$args 				= func_get_args(); array_shift($args);
		$n 					= count($args);
		$tokens				= array();
		for($i=0;$i<$n;++$i)
		{
			$tokens['{'.$i.'}'] = $args[$i];
		}
		parent::__construct(strtr($errorMessage,$tokens));
	}
	/**
	 * 解释是错误代码还是错误提示.
	 * @param string error code that is passed in the exception constructor.
	 * @return string the translated error message
	 */
	protected function translateErrorMessage($key)
	{
		$msgFile = $this->getErrorMessageFile();
		if(($entries = file($msgFile))===false)
		{
			return $key;
		}
		else
		{
			foreach($entries as $entry)
			{
				@list($code,$message)=explode('=',$entry,2);
				if(trim($code)===$key)
				{
					return trim($message);
				}
			}
			return $key;
		}
	}
	/**
	 * 获取错误提示模板文件
	 * @return string path to the error message file
	 */
	protected function getErrorMessageFile()
	{
		$msgFile = BASEPATH."/languages/{$this->lang}/messages.txt";
		return $msgFile;
	}
	/**
	 * 获取错误代码
	 * @return string error code
	 */
	public function getErrorCode()
	{
		return $this->_errorCode;
	}
	/**
	 * 设置错误代码
	 * @param string error code
	 */
	public function setErrorCode($code)
	{
		$this->_errorCode = $code;
	}
	/**
	 * 获得错误提示
	 * @return string error message
	 */
	public function getErrorMessage()
	{
		return $this->getMessage();
	}
	/**
	 * 设置错误提示
	 * @param string error message
	 */
	protected function setErrorMessage($message)
	{
		$this->message = $message;
	}
}
/**
 * 系统级异常
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TSystemException extends TException
{
}
/**
 * TApplicationException class
 *
 * TApplicationException
 *
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TApplicationException extends TException
{
}
/**
 * 非法操作
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TInvalidOperationException extends TSystemException
{
}
/**
 * 配置异常
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TConfigurationException extends TSystemException
{
}

/**
 * 模板异常类
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TTemplateException extends TConfigurationException
{
	private $_template		= '';
	private $_lineNumber	= 0;
	private $_fileName		= '';

	/**
	 * @return string the template source code that causes the exception.
	 * This is empty if {@link getTemplateFile TemplateFile} is not empty.
	 */
	public function getTemplateSource()
	{
		return $this->_template;
	}
	/**
	 * @param string the template source code that causes the exception
	 */
	public function setTemplateSource($value)
	{
		$this->_template = $value;
	}
	/**
	 * @return string the template file that causes the exception. This could be empty if the template is an embedded template. In this case, use {@link getTemplateSource TemplateSource} to obtain the actual template content.
	 */
	public function getTemplateFile()
	{
		return $this->_fileName;
	}
	/**
	 * @param string the template file that causes the exception
	 */
	public function setTemplateFile($value)
	{
		$this->_fileName = $value;
	}
	/**
	 * @return integer the line number at which the template has error
	 */
	public function getLineNumber()
	{
		return $this->_lineNumber;
	}
	/**
	 * @param integer the line number at which the template has error
	 */
	public function setLineNumber($value)
	{
		$this->_lineNumber = intval($value);
	}
}
/**
 * 数据库异常处理类
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TDbException extends TSystemException
{
}
/**
 * PHP异常处理
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class TPhpErrorException extends TSystemException
{
	/**
	 * 构造函数.
	 * @param integer error number
	 * @param string error string
	 * @param string error file
	 * @param integer error line number
	 */
	public function __construct($errno,$errstr,$errfile,$errline)
	{
		static $errorTypes = array(
		E_ERROR           => "Error",
		E_WARNING         => "Warning",
		E_PARSE           => "Parsing Error",
		E_NOTICE          => "Notice",
		E_CORE_ERROR      => "Core Error",
		E_CORE_WARNING    => "Core Warning",
		E_COMPILE_ERROR   => "Compile Error",
		E_COMPILE_WARNING => "Compile Warning",
		E_USER_ERROR      => "User Error",
		E_USER_WARNING    => "User Warning",
		E_USER_NOTICE     => "User Notice",
		E_STRICT          => "Runtime Notice");
		$errorType = isset($errorTypes[$errno])?$errorTypes[$errno]:'Unknown Error';
		parent::__construct("[$errorType] $errstr (@line $errline in file $errfile).");
	}
}
/**
 * HTTP异常处理类
 * @author BoPo <ibopo@126.com>
 * @version $Id $
 * @package exception
 * @since 0.10
 */
class THttpException extends TSystemException
{
	private $_statusCode;
	/**
	 * 构造函数.
	 * @param integer HTTP status code, such as 404, 500, etc.
	 * @param string error message. This can be a string that is listed
	 */
	public function __construct($statusCode,$errorMessage)
	{
		$this->_statusCode = $statusCode;
		$this->setErrorCode($errorMessage);
		$errorMessage = $this->translateErrorMessage($errorMessage);
		$args 	= func_get_args();
		array_shift($args);
		array_shift($args);
		$n 		= count($args);
		$tokens = array();
		for($i=0;$i<$n;++$i)
		{
			$tokens['{'.$i.'}'] = $args[$i];
		}
		parent::__construct(strtr($errorMessage,$tokens));
	}
	/**
	 * @return integer HTTP status code, 404, 500
	 */
	public function getStatusCode()
	{
		return $this->_statusCode;
	}
}
/*
+--------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Fri Nov 16 16:25:22 CST 2007
+--------------------------------------------------------------------------
*/
?>